<template>
  <el-dialog :title="'人员设置--'+userGroupName" :visible="visible" width="850px" @close="close">
    <el-container v-loading="loading" class="tab-container">
      <header class="header">
        <el-form
          ref="queryCondition"
          :model="queryCondition"
          inline
          class="header-search_form"
          @keyup.enter.native="handleQuery"
        >
          <el-form-item>
            <p-input v-model="queryCondition.account" type="text" placeholder="账号" />
          </el-form-item>
          <el-form-item>
            <p-input v-model="queryCondition.name" type="text" placeholder="姓名" />
          </el-form-item>
          <el-form-item label="状态">
            <dictionary-select
              v-model="queryCondition.status"
              multiple
              :code="constant.DICTIONARY_TYPE_CODES.USER_STATUS"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              v-permission="'system:userGroup:user'"
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
            >查询</el-button>
          </el-form-item>
        </el-form>
      </header>
      <el-main style="padding:0px;margin-top:0px;">
        <div style="margin-top:10px;">
          <el-button
            v-permission="'system:userGroup:user'"
            type="primary"
            style="margin-left:0px;"
            size="small"
            icon="el-icon-plus"
            @click="add"
          >新增</el-button>
          <el-button
            v-permission="'system:userGroup:user'"
            type="primary"
            style="margin-left:20px;"
            size="small"
            icon="el-icon-delete"
            @click="batchRemove"
          >移除</el-button>
        </div>
        <el-table
          :data="tableData"
          stripe
          highlight-current-row
          border
          style="padding:0px;margin-top:10px;"
          @sort-change="handleSortChange"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50px" />
          <el-table-column prop="account" label="账号" />
          <el-table-column prop="name" label="姓名" />
          <el-table-column prop="statusName" label="状态" />
          <el-table-column prop="organizationName" label="组织机构" />
        </el-table>
        <el-pagination
          :current-page="pageInfo.pageNum"
          :level1-sizes="constant.DEFAULT_PAGE_SIZE_ARR"
          :level1-size="pageInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageTotal"
          style="margin-top:10px;"
          @size-change="handlePageSize"
          @current-change="handlePage"
        />
      </el-main>
      <el-footer style="padding:0px;margin-top:10px;">
        <el-button type="primary" style="float:right;" @click="close">关闭</el-button>
      </el-footer>
    </el-container>
    <add-user ref="addUser" @ok="loadData" />
  </el-dialog>
</template>

<script>
import PInput from '@/components/popsoft/Query/PInput'
import DictionarySelect from '@/components/popsoft/DictionarySelect/DictionarySelect'
import addUser from './addUser'
export default {
  name: 'userGroup-user',
  components: { PInput, DictionarySelect, addUser },
  data() {
    return {
      userGroupId: '',
      userGroupName: '',
      tableData: [],
      loading: false,
      pageTotal: 0,
      pageInfo: {
        pageNum: this.constant.DEFAULT_PAGE_NUM,
        pageSize: this.constant.DEFAULT_PAGE_SIZE
      },
      sortInfo: {
        sort_field: 'account',
        sort_sortType: 'ascending'
      },
      queryCondition: {
        name: '',
        account: ''
      },
      multipleSelection: [],
      visible: false
    }
  },
  methods: {
    show(id, name) {
      this.userGroupId = id
      this.userGroupName = name
      this.loadData(id)
      this.visible = true
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    loadData() {
      this.loading = true
      const params = Object.assign(this.queryCondition, this.pageInfo, this.sortInfo)
      this.$api.system.user.getUserGroupUser(this.userGroupId, params)
        .then(res => {
          this.tableData = res.data.records
          this.pageTotal = res.data.total
        }).finally(() => {
          this.loading = false
        })
    },
    handlePage(value) {
      this.pageInfo.pageNum = value
      this.loadData()
    },
    handlePageSize(value) {
      this.pageInfo.pageSize = value
      this.loadData()
    },
    handleQuery() {
      this.pageInfo.pageNum = 1
      this.loadData()
    },
    close() {
      this.visible = false
    },
    add() {
      this.$refs.addUser.show(this.userGroupId)
    },
    batchRemove() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'info',
          message: '请至少选中一行'
        })
        return
      }
      this.$confirm('确定将所选用户从该用户组中移除吗?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.system.userGroup.batchRemoveUser(this.userGroupId, { idList: this.multipleSelection.map(row => row.id) })
          .then(() => {
            this.loadData()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 处理排序
    // eslint-disable-next-line no-unused-vars
    handleSortChange({ column, prop, order }) {
      this.sortInfo.sort_field = prop
      this.sortInfo.sort_sortType = order
      this.refresh()
    }
  }
}
</script>

<style lang='scss' scoped>
.tab-container {
  padding: 15px;
  margin-left: 30px;
  width: 800px;
  .header {
    height: 50px !important;
    .header-search_form {
      height: 40px;
    }
  }
}
</style>
